package com.quan.admin.configuration;

import com.quan.admin.commons.constant.RabbitMQConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class SysRabbitMQConfig {

    /* 容器中的Queue、Exchange、Binding 会自动创建（在RabbitMQ）不存在的情况下 */

    /**
     * TopicExchange 交换机
     *
     * @return
     */
    @Bean
    public Exchange systemEventExchange() {
        /*
         *   String name,
         *   boolean durable,
         *   boolean autoDelete,
         *   Map<String, Object> arguments
         * */
        return new TopicExchange(RabbitMQConstant.SYSTEM_EVENT_EXCHANGE, true, false);
    }

    /**
     * 邮件发送队列
     * @return
     */
    @Bean
    public Queue sendMailQueue() {
        Queue queue = new Queue(RabbitMQConstant.SYSTEM_QUEUE_MAIL, true, false, false);
        return queue;
    }

    /**
     * 绑定队列到交换机
     * 发送邮件
     * @return
     */
    @Bean
    public Binding sendMailCreateBinding() {
        /*
         * String destination, 目的地（队列名或者交换机名字）
         * DestinationType destinationType, 目的地类型（Queue、Exhcange）
         * String exchange,
         * String routingKey,
         * Map<String, Object> arguments
         * */
        return new Binding(RabbitMQConstant.SYSTEM_QUEUE_MAIL,
                Binding.DestinationType.QUEUE,
                RabbitMQConstant.SYSTEM_EVENT_EXCHANGE,
                RabbitMQConstant.SYSTEM_ROUTING_KEY_MAIL,
                null);
    }


    /**
     * 即时消息保存队列
     * @return
     */
    @Bean
    public Queue messageQueue() {
        Queue queue = new Queue(RabbitMQConstant.SYSTEM_QUEUE_MESSAGE, true, false, false);
        return queue;
    }

    /**
     * 绑定队列到交换机
     * 即时消息保存
     * @return
     */
    @Bean
    public Binding messageCreateBinding() {
        /*
         * String destination, 目的地（队列名或者交换机名字）
         * DestinationType destinationType, 目的地类型（Queue、Exhcange）
         * String exchange,
         * String routingKey,
         * Map<String, Object> arguments
         * */
        return new Binding(RabbitMQConstant.SYSTEM_QUEUE_MESSAGE,
                Binding.DestinationType.QUEUE,
                RabbitMQConstant.SYSTEM_EVENT_EXCHANGE,
                RabbitMQConstant.SYSTEM_ROUTING_KEY_MESSAGE,
                null);
    }




}
